﻿using BCL.BaseLib;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Windows.Forms;
using BCL.BaseLib.Basics;
using System.Drawing;
using System.IO;

namespace DevicesView.Logic.App
{
    public class FunctionLogic
    {
        public static List<FunctionEntity> GetFunctions()
        {
            //string url = AppResource.LogicServerAddress + string.Format("SystemUserApps/{0}/{1}", AppResource.CurrentResource.AppCode, AppResource.CurrentResource.LoginUser.UserCode);
            //HttpItem hi = new HttpItem { Url = url, Method = HttpMethod.Get };
            //HttpResult<string> result = WebLogic.GetHttpResult<string>(hi);
            //if (result.Success)
            //{
            //    List<FunctionEntity> webfes = JsonConvert.DeserializeObject<List<FunctionEntity>>(result.Result);
            //    if (webfes != null)
            //        webfes.ForEach(o => SetFunctionImage(o));
            //    return webfes;
            //}


            MemoryStream image7 = new MemoryStream(File.ReadAllBytes("image\\7.png"));
            List<FunctionEntity> fes = new List<FunctionEntity>();

            FunctionEntity fe = new FunctionEntity { AppCode = "001", AppId = "001", Title = "检查化验", ItemType = FunctionItemType.Group };
            fe.Items = new List<FunctionEntity>
            {
               new FunctionEntity { AppCode = "00201", AppId = "00201", Title = "待检查",Index=true, ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "XiaonuanPMS.dll", ClassFillName = "XiaonuanPMS.UCTestNew" }, ImagePath="image\\2.png" },
                new FunctionEntity { AppCode = "00101", AppId = "00101", Title = "已完成", StartLoad=true, ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.UCRelust" },ImagePath="image\\1.png" }
                // new FunctionEntity { AppCode = "00101", AppId = "00101", Title = "页面测试", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "XiaonuanPMS.dll", ClassFillName = "XiaonuanPMS.UCControl.UCTestItems" },ImagePath="image\\1.png" },
            };
            fes.Add(fe);

            FunctionEntity fe2 = new FunctionEntity { AppCode = "002", AppId = "002", Title = "系统设置", ItemType = FunctionItemType.Group };
            fe2.Items = new List<FunctionEntity>
            {
                new FunctionEntity { AppCode = "00105", AppId = "00105", Title = "设备管理", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "DevicesWPF.dll", ClassFillName = "DevicesWPF.UCDevicesCollectionConfig" }, ImagePath="image\\3.png" },
                // new FunctionEntity { AppCode = "00102", AppId = "00102", Title = "设备管理", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Function, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.Config.DevicesConfig" }, ImagePath="image\\3.png" },
                new FunctionEntity { AppCode = "00103", AppId = "00103", Title = "医院信息", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.Config.UCOrgConfig" }, ImagePath="image\\5.png" },
                new FunctionEntity { AppCode = "00104", AppId = "00104", Title = "打印设置", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.Config.UCPrintConfig" }, ImagePath="image\\4.png" },

                //new FunctionEntity { AppCode = "00102", AppId = "00102", Title = "小暖", Index = true, ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "XiaonuanPMS.dll", ClassFillName = "XiaonuanPMS.FormTestNew" }, Image = image2 },
                //new FunctionEntity { AppCode = "00201", AppId = "00201", Title = "检查", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.FormTestNew" }, Image = image2 },
                //new FunctionEntity { ItemParamter = "http://www.huimopei.com:8888/management", AppCode = "00202", AppId = "00202", Title = "打印", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.Web.WebControl" }, Image = image2 },
                //new FunctionEntity { ItemParamter = "http://localhost:8080/management", Index = true, AppCode = "00202", AppId = "00202", Title = "设备管理", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.Web.WebControl" }, Image = image2 },
                //new FunctionEntity { ItemParamter = "http://www.huimopei.com:8888", Index = true, AppCode = "00203", AppId = "00203", Title = "网页2", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.Web.WebControl" }, Image = image2 },
                //new FunctionEntity { ItemParamter = @"file:\\\D:\werkspaces\Lis\Lis.core\app\Debug\dist\index.html#/management", Index = true, AppCode = "002067", AppId = "0020345", Title = "网页34", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "View.dll", ClassFillName = "DevicesView.Web.WebControl" }, Image = image2 }
            };
            fes.Add(fe2);

            FunctionEntity fe3 = new FunctionEntity { AppCode = "003", AppId = "003", Title = "系统设置", ItemType = FunctionItemType.Group };
            fe3.Items = new List<FunctionEntity>
            {
                new FunctionEntity { AppCode = "00303", AppId = "00303", Title = "技术支持", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "WpfControlTool.dll", ClassFillName = "WpfControlTool.Controls.UCAboutUs" }, ImagePath="image\\7.png" },
                 new FunctionEntity { AppCode = "00303", AppId = "00304", Title = "系统信息", ItemType = FunctionItemType.Item, FunctionType = FunctionType.Control, ClassInfo = new ClassInfo { DLLName = "WpfControlTool.dll", ClassFillName = "WpfControlTool.Controls.UCAppConfig" }, ImagePath="image\\8.png" },
            };
            fes.Add(fe3);



            //随程序启动后台运行的应用
            FunctionEntity fe100 = new FunctionEntity { IsCreate = false, Title = "随程序启动，后台运行", ItemType = FunctionItemType.Group };
            fe100.Items = new List<FunctionEntity>
            {
                new FunctionEntity { IsCreate=false, AppCode = "00102", AppId = "00102", StartLoad = true, ItemType = FunctionItemType.Item, FunctionType = FunctionType.Function, ClassInfo = new ClassInfo { DLLName = "Logic.dll", ClassFillName = "DevicesView.Logic.Test.CommandLogic" } },
                new FunctionEntity { IsCreate=false, AppCode = "00103", AppId = "00102", StartLoad = true, ItemType = FunctionItemType.Item, FunctionType = FunctionType.Function, ClassInfo = new ClassInfo { DLLName = "WpfControlTool.dll", ClassFillName = "WpfControlTool.Logic.TopControl" } }
            };

            fes.Add(fe100);

            return fes;
        }

        /// <summary>
        /// 获取前置菜单
        /// </summary>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static List<FunctionEntity> GetApplicationFunctions(int appid)
        {
            //return null;
            //前置启动项
            List<FunctionEntity> fes = new List<FunctionEntity>()
            {
               new FunctionEntity{ItemType= FunctionItemType.Item, FunctionType= FunctionType.Function, StartLoad=true, ClassInfo= new ClassInfo{ DLLName="WpfControlTool.dll", ClassFillName="WpfControlTool.Logic.Login" } }
            };
            return fes;
        }


        public static bool StartAppLicationFunctions()
        {
            List<FunctionEntity> functionEntities = GetApplicationFunctions(3);
            if (functionEntities != null && functionEntities.Count > 0)
            {
                foreach (var item in functionEntities)
                    if (item.Custom?.FunctionInvoke(item) == false)
                        return false;
            }

            return true;
        }


        private static void SetFunctionImage(FunctionEntity fe)
        {
            if (!fe.IsCreate)
                return;
            if (fe.Image == null && !string.IsNullOrEmpty(fe.ImagePath))
                fe.Image = new System.IO.MemoryStream(ImageTool.LoadFile(fe.ImagePath));
            fe.Items?.ForEach(o => SetFunctionImage(o));
        }

    }
}
